package br.inf.mfpo.fra4and.exemplo.provider;

import java.util.List;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import br.inf.mfpo.fra4and.exemplo.columns.CoisasColumns;
import br.inf.mfpo.fra4and.exemplo.columns.PessoasColumns;

public class Fra4AndProvider extends ContentProvider {
	public static final String AUTHORITY = "br.inf.mfpo.fra4and.exemplo.fra4andcontentprovider";
	public static final String TAG = "Fra4AndProvider";

	private static final int PESSOAS = 10;
	private static final int PESSOAS_ID = 11;
	private static final int COISAS = 20;
	private static final int COISAS_ID = 21;

	private static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

		uriMatcher.addURI(AUTHORITY, PessoasColumns.TAG, PESSOAS);
		uriMatcher.addURI(AUTHORITY, CoisasColumns.TAG, COISAS);

		uriMatcher.addURI(AUTHORITY, PessoasColumns.TAG + "/#", PESSOAS_ID);
		uriMatcher.addURI(AUTHORITY, CoisasColumns.TAG + "/#", COISAS_ID);
	}

	private DatabaseHelper dbHelper;

	@Override
	public boolean onCreate() {
		dbHelper = new DatabaseHelper(getContext());
		return dbHelper.getWritableDatabase() != null;
	}

	@Override
	public String getType(Uri uri) {

		switch (uriMatcher.match(uri)) {
		case PESSOAS:
			return PessoasColumns.CONTENT_TYPE;
		case PESSOAS_ID:
			return PessoasColumns.CONTENT_ITEM_TYPE;
		case COISAS:
			return CoisasColumns.CONTENT_TYPE;
		case COISAS_ID:
			return CoisasColumns.CONTENT_ITEM_TYPE;
		default:
			throw new IllegalArgumentException("Unknown URL " + uri);
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		int count = 0;
		switch (uriMatcher.match(uri)) {
		case PESSOAS:
			count = dbHelper.deletePessoas(selection, selectionArgs);
			notifyChanges(uri);
			return count;
		case PESSOAS_ID:
			count = dbHelper.deletePessoas(uri.getPathSegments().get(1));
			notifyChanges(uri);
			return count;
		case COISAS:
			count = dbHelper.deleteCoisas(selection, selectionArgs);
			notifyChanges(uri);
			return count;
		case COISAS_ID:
			count = dbHelper.deleteCoisas(uri.getPathSegments().get(1));
			notifyChanges(uri);
			return count;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		if (values == null) {
			values = new ContentValues();
		}

		Uri result = null;

		switch (uriMatcher.match(uri)) {
		case PESSOAS:
			result = dbHelper.insertPessoas(values);
			notifyChanges(uri);
			return result;
		case COISAS:
			result = dbHelper.insertCoisas(values);
			notifyChanges(uri);
			return result;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		Cursor result = null;

		String id = null;
		final List<String> pathSegments = uri.getPathSegments();
		if (pathSegments.size() > 1) {
			id = uri.getPathSegments().get(1);
		}

		switch (uriMatcher.match(uri)) {
		case PESSOAS:
			result = dbHelper.queryPessoas(projection, selection, selectionArgs, sortOrder);
			result.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case PESSOAS_ID:
			result = dbHelper.queryPessoas(id, projection, sortOrder);
			result.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case COISAS:
			result = dbHelper.queryCoisas(projection, selection, selectionArgs, sortOrder);
			result.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case COISAS_ID:
			result = dbHelper.queryCoisas(id, projection, sortOrder);
			result.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		default:
			result = null;
		}

		if (result == null) {
			throw new IllegalArgumentException("URI not found");
		}

		return result;
	}

	@Override
	public int update(Uri uri, ContentValues values, String where, String[] whereArgs) {
		if (values == null) {
			throw new IllegalArgumentException("Values can not be null, for " + uri);
		}

		String id = null;
		final List<String> pathSegments = uri.getPathSegments();
		if (pathSegments.size() > 1) {
			id = uri.getPathSegments().get(1);
		}

		int count = 0;
		switch (uriMatcher.match(uri)) {
		case PESSOAS:
			count = dbHelper.updatePessoas(values, where, whereArgs);
			notifyChanges(uri);
			return count;
		case PESSOAS_ID:
			count = dbHelper.updatePessoas(id, values);
			notifyChanges(uri);
			return count;
		case COISAS:
			count = dbHelper.updateCoisas(values, where, whereArgs);
			notifyChanges(uri);
			return count;
		case COISAS_ID:
			count = dbHelper.updateCoisas(id, values);
			notifyChanges(uri);
			return count;
		default:
			throw new IllegalArgumentException("Unknown URI " + uri);
		}
	}

	private void notifyChanges(Uri uri) {
		getContext().getContentResolver().notifyChange(uri, null, true);
	}
}
